<template>
  <div>
    <el-form :model="ruleForm" ref="ruleForm" class="ruleForm">
      <table
        border="1"
        cellpadding
        style="width: 100%"
        cellpadding="0"
        cellspacing="0"
      >
        <thead>
        <tr style="height: 44px">
          <th :colspan="2" width="150">项目</th>
          <th width="150">限度</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(val, ind) in ruleForm.table_info" :key="ind" style="height: 44px">
          <td v-if="val.boo" align="center" :colspan="val.col" :rowspan="val.row" :width="val.boo ? '50' : '200'">
            {{ val.parent_name }}
            <el-button
              v-if="val.col === 2 && val.limit_commit === 1"
              type="text"
              class="el-icon-plus"
              @click="handlAdd(ind)"
            ></el-button>
          </td>
          <td v-if="val.col === 1" :colspan="val.col" width="150">
            <el-form-item :prop="'table_info.' + ind + '.children_name'"
                          :rules="[ { required: true, message: '请输入内容', trigger: 'blur' }]">
              <div style="display: flex">
                <el-input v-model="val.children_name">
                </el-input>
                <el-button style="width: 20px" type="text" icon="el-icon-plus"
                           @click="handAddCol(ind,val.parent_id)"></el-button>
                <el-button style="width: 20px" type="text" icon="el-icon-minus"
                           @click="handDeleCol(ind,val.parent_id,val.row)"></el-button>
              </div>

            </el-form-item>
          </td>
          <td align="center" width="150">
            <el-form-item :prop="'table_info.' + ind + '.limit_value'"
                          :rules="[ { required: true, message: '请输入内容', trigger: 'blur' }]">
              <el-input v-if="val.limit_commit === 1" v-model="val.limit_value"/>
              <span v-else>{{val.limit_value}}</span>
            </el-form-item>
          </td>
        </tr>
        </tbody>
      </table>
      <el-button @click="submitFormsss" style="margin-top: 20px;float: right" type="primary">提交</el-button>
    </el-form>
  </div>
</template>

<script>
  import { sampleSetTable } from '@/api/sampleInspection'

  export default {
    name: 'setTable',
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {
            table_info: []
          }
        }
      },
      planId: {
        type: String,
        default() {
          return ''
        }
      }
    },
    data() {
      return {}
    },
    //是计算属性，也可以理解为一个方法。
    computed: {},

    //属性是手动定义的所需监听的值
    watch: {},

    //生命周期 - 创建完成（访问当前this实例）
    created() {
    },

    //生命周期 - 挂载完成（访问DOM元素）
    mounted() {

    },

    //是Vue实例对象上绑定的方法，供当前Vue组件作用域内使用，
    methods: {

      handlAdd(index) {
        this.ruleForm.table_info[index].col = 1
      },
      handAddCol(index, id) {
        let bbb = JSON.parse(JSON.stringify(this.ruleForm.table_info[index]))
        let aaa = {}
        let idx = index + 1
        let data = {
          boo: false,
          children_id: '',
          children_name: '',
          limit_commit: 1,
          limit_value: '',
          parent_id: bbb.parent_id,
          parent_name: bbb.parent_name,
          point_info: bbb.point_info
        }
        this.ruleForm.table_info[index].row = this.ruleForm.table_info[index].row + 1
        this.ruleForm.table_info.splice(idx, 0, data)
        this.ruleForm.table_info[idx].row = this.ruleForm.table_info[index].row
        this.ruleForm.table_info.filter(a => a.parent_id === id).forEach(item => {
          item.row = this.ruleForm.table_info[idx].row
        })
        this.ruleForm.table_info[idx].col = 1
        console.log(this.ruleForm.table_info, '-=-==-=-=-==-')
      },

      handDeleCol(index, id, row) {
        let aaa = this.ruleForm.table_info.filter(a => a.parent_id === this.ruleForm.table_info[index].parent_id)
        aaa = JSON.parse(JSON.stringify(aaa))
        if (aaa.length === 1) {
          let a = JSON.parse(JSON.stringify(this.ruleForm.table_info[index]))
          a.row = 1
          a.col = 2
          this.ruleForm.table_info.splice(index, 1)
          this.ruleForm.table_info.splice(index, 0, a)
        } else {
          this.ruleForm.table_info.splice(index, 1)
          let arr = []
          for (let i = 0; i < this.ruleForm.table_info.length; i++) {
            let obj = this.ruleForm.table_info[i]
            if (obj.parent_id === id) {
              arr.push(i)
              obj.row = row - 1
            }
          }
          let as = arr[0]
          if (this.ruleForm.table_info[as].boo === false) {
            this.$set(this.ruleForm.table_info[as], 'boo', true)
          }
        }

      },

      submitFormsss() {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.handlSetTable(this.ruleForm.table_info)
          }
        })

      },

      handlSetTable(row) {
        sampleSetTable({ plan_id: this.planId, result: row }).then(res => {
          if (res.code === 200) {
            this.$emit('successTable')
          }
        })

      }

    }
  }

</script>

<style scoped>
  .ruleForm ::v-deep .el-form-item {
    margin-bottom: 0 !important;
  }

  ::v-deep .ruleForm .el-input-group__append {
    padding: 0 !important;
  }
</style>

